<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="短信内容" prop="templateParam">
        <el-input
          v-model="queryParams.templateParam"
          placeholder="请输入短信内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['message:content:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['message:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['message:content:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['message:content:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20">
    <el-col :lg="6" v-for="(it,index) in contentList" :key="index">
      <el-card class="box-card">
      <div slot="header" class="clearfix text">
        <span>入会短信</span>
       
      </div>
      <div class="text item">
        <p>短信内容:{{it.templateParam}}</p>
        <p>创建时间：{{it.createTime}}</p>
        <el-button type="warning" size="mini" style="float:right; margin-right: 10px;" @click="handleUpdate(it)">立即发送</el-button>
        <el-button type="primary" size="mini" style="float:right; margin-right: 10px;" icon="el-icon-delete"></el-button>
      </div>
    </el-card>
    </el-col>
    </el-row>
    <!-- 添加或修改短信内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="短信内容" prop="templateParam">
          <el-input type="textarea" autosize v-model="form.templateParam" placeholder="请输入短信内容" />
        </el-form-item>
        <el-form-item label="跳转公众号" prop="appId">
          <el-select v-model="form.appId" placeholder="请选择">
          <el-option
            v-for="item in smsAppOption"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="短信模板" prop="templateCode">
        <el-select v-model="form.templateCode" placeholder="请选择">
          <el-option
            v-for="item in smsOption"
            :key="item.id"
            :label="item.signName"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="接收的用户" prop="concatId">
          <el-form :model="userQueryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="name">
        <el-input
          v-model="userQueryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleConcatQuery"
        />
      </el-form-item>
      <el-form-item label="绑定状态" prop="realName">
        <el-input
          v-model="userQueryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleConcatQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleConcatQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="wxContactsList" @selection-change="handleSelectionChange" border  ref="multipleTable" :row-key="getRowKey">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="会内职务" align="center" prop="internalJob" />
      <el-table-column label="手机" align="center" prop="mobile" />
      <el-table-column label="是否入会" align="center" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.openid==null" type="warning">已入会</el-tag>
          <el-tag v-else type="info">未入会</el-tag>
        </template>
      </el-table-column>
      </el-table>
      <pagination
      v-show="total>0"
      :total="total"
      :page.sync="userQueryParams.pageNum"
      :limit.sync="userQueryParams.pageSize"
      @pagination="getContcatList"
    />
    </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">发送短信</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/message/content";
import { listWxContacts } from "@/api/allocation/wxContacts";
export default {
  name: "Content",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短信内容表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      smsOption:[],
      smsAppOption:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateParam: null,
        templateCode: null,
        concatId: null,
        status: null,
        appId: null,
        asId: null
      },
      //选中通讯录数组
      oldConcatIds:[],
      //微信通讯录数组
      wxContactsList:[],
      userQueryParams:{
        pageNum: 1,
        pageSize: 10,
        asId:null,
        name:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getContcatList();
  },
  methods: {
    getRowKey(row){
      return row.id;
    },
    createIndex(index){
      return (this.userQueryParams.pageNum-1)*(this.userQueryParams.pageSize)+index+1;
    },
    /** 查询短信内容列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(res => {
        this.contentList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    getContcatList(){
      listWxContacts(this.userQueryParams).then(res=>{
        this.wxContactsList=res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        templateParam: null,
        templateCode: null,
        concatId: null,
        status: 0,
        createTime: null,
        updateTime: null,
        appId: null,
        asId: null,
        concatIds:[]
      };
      this.resetForm("form");
    },
    handleConcatQuery(){
      this.userQueryParams.pageNum = 1;
      this.getContcatList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加短信内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(res => {
        this.form = res.smsContent;
        this.smsOption=res.list;
        this.smsAppOption=res.smsApps;
        this.oldConcatIds=res.smsContent.concatIds;
        console.info(this.wxContactsList);
        if(this.oldConcatIds.length>0){
           for(let i=0;i<this.oldConcatIds.length;i++){
            for(let j=0;j<this.wxContactsList.length;j++){
              console.info(this.wxContactsList[j])
              if(this.oldConcatIds[i]==this.wxContactsList[j].id){
                this.$refs.multipleTable.toggleRowSelection(this.wxContactsList[j]);
              }
            }
           }
        }
        this.open = true;
        this.title = "修改短信内容";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.concatIds=this.ids;
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除短信内容编号为"' + ids + '"的数据项？').then(function() {
        return delContent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('message/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
   width: auto;
  }
</style>
